import operate as ope
import run_logging as log
import time
import notice
import message as msg
import sys

work_image_path = '../../image/work\\'
mon_image_path = '../../image/monitor\\'
ope_image_path = '../../image/operate\\'


def monitor():
    # ope.random_sleep(10, 0)
    red_count = 0
    ope.click_position(700, 150)
    while True:
        # 判定服务器时间是否在十点五十到十点五十五
        stop_times = [50, 51, 52, 53, 54, 55]
        current_time = time.localtime()
        if current_time.tm_hour == 10 and current_time.tm_min in stop_times:
            check_back()
            break
        time.sleep(1)
        # 检查是否安全
        if ope.is_clear():  # 安全
            # 检查是否在空间站中
            if ope.is_in_station():  # 在空间站
                # 检查当前是否在空间站中
                ope.click_image_path(ope_image_path + 'out_station.png')
                log.log('即将出站')
                ope.random_sleep(10, 2)
                repair = ope.find_image(mon_image_path + 'repair.png')
                while repair is not None:
                    ope.click_image(repair)
                    break
                ope.click_image(ope.find_image(mon_image_path + 'overview_work.png'))
            else:  # 不在空间站
                ope.random_sleep(2, 1)
        elif ope.is_clear() is False:  # 不安全
            check_back_emc()
            red_count += 1
            log.log(f'第{red_count}次红名进入')
            msg.send_msg(f'第{red_count}次红名进入', '胜利丁')
            #  等待5分钟
            ope.random_sleep(300, 100)
        # 空护盾
        elif ope.find_image(mon_image_path + 'need_repair.png') is not None:
            check_back_emc()
            """msg.send_msg('需要维修')
            msg.send_image()"""
            log.log('需要维修')
            msg.send_msg('需要维修', '胜利丁')
            notice.beep()
        # 布瑞恩
        elif ope.find_image(mon_image_path + 'Bryant.png') is not None:
            check_back_emc()
            """msg.send_msg('布瑞恩')
            msg.send_image()"""
            log.log('布莱恩')
            msg.send_msg('布莱恩', '胜利丁')
            notice.beep()
        elif ope.find_image(mon_image_path + 'is_alive.png') is not None:
            ope.back_station()
            """msg.send_msg('变蛋咯')
            msg.send_image()"""
            log.log('变蛋咯')
            msg.send_msg('变蛋咯', '胜利丁')
            notice.beep()
        ope.random_sleep(3, 1)


def check_back_emc():
    #  判定当前是否在空间站中
    if ope.is_in_station() is False:  # 在太空中
        #  判定太空是否有无人机
        if ope.is_UAV_back() is False:  # 有无人机
            ope.call_back_UVA()
            log.log('正在收回无人机')
            # 朝向并收回无人机
            ope.random_sleep(0.5, 0.2)
            ope.towards_station()
            i = 0
            #  收无人机朝向等无人机总览没内容后起跳
            #  等五到十秒强制起跳
            while i < 15:
                i += 1
                ope.random_sleep(1, 0.2)
                print(i)
                if ope.is_UAV_back():
                    log.log('已收回全部无人机')
                    break
        else:
            #  没有无人机直接起跳
            log.log('没有找到无人机')
        # 起跳空间站
        ope.back_station()
        ope.random_sleep(5, 1)
        ope.click_image_path(mon_image_path + 'overview_general.png')


def check_back():
    #  判定当前是否在空间站中
    if ope.is_in_station() is False:  # 在太空中
        #  判定太空是否有无人机
        if ope.is_UAV_back() is False:  # 有无人机
            log.log('正在收回无人机')
            # 朝向并收回无人机
            ope.random_sleep(1, 0.5)
            ope.towards_station()
            ope.call_back_UVA()
            while ope.is_UAV_back() is False:
                if ope.is_UAV_back():
                    ope.back_station()
                    log.log('已收回全部无人机')
        else:
            #  没有无人机直接起跳
            log.log('没有找到无人机')
        # 起跳空间站
        ope.back_station()
        ope.random_sleep(5, 1)
        ope.click_image_path(mon_image_path + 'overview_general.png')

monitor()
